home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / CH_2.2 / HISTRAMP / histdistr.c next >
Text File  |  1999-09-11  |  1KB  |  51 lines

  1. /* 
  2.  * histdistr.c
  3.  * 
  4.  * Practical Algorithms for Image Analysis
  5.  * 
  6.  * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
  7.  */
  8.  
  9. /* HISTDISTR:   function calculates the MODIFIED histogram cumulative
  10.  *            distribution, that is, from the lowest occupied bin
  11.  *              to the highest, MINUS 1/2 OF THESE LOWEST AND HIGHEST BINS
  12.  *                      usage: histdistr (hist, nBins, distr)
  13.  *
  14.  *
  15.  */
  16.  
  17. int
  18. histdistr (hist, nBins, distr)
  19.      long *hist,                /* histogram array */
  20.        nBins,                   /* no. of histogram bins */
  21.       *distr;                   /* histogram cumulative distr. array */
  22. {
  23.   register long binLow,         /* lowest and highest occupied bins */
  24.     binHigh, bin;               /* histogram bin incrementor */
  25.  
  26.   double total;                 /* floating point cumulative distr. */
  27.  
  28. /* find lowest and highest occupied bins */
  29.   for (binLow = 0; binLow < nBins; binLow++)
  30.     if (hist[binLow] != 0)
  31.       break;
  32.   for (binHigh = nBins - 1; binHigh >= 0; binHigh--)
  33.     if (hist[binHigh] != 0)
  34.       break;
  35.  
  36. /* compute cumulative distribution */
  37.   for (bin = 0; bin <= binLow; bin++)
  38.     distr[bin] = 0;
  39.  
  40.   total = 0.0;
  41.   for (bin = binLow + 1; bin <= binHigh; bin++) {
  42.     total += ((hist[bin - 1] + hist[bin]) / 2.0);
  43.     distr[bin] = (long) (total + 0.5);
  44.   }
  45.  
  46.   for (bin = binHigh + 1; bin < nBins; bin++)
  47.     distr[bin] = distr[bin - 1];
  48.  
  49.   return (0);
  50. }
  51.